/*
Count and Say

The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...

1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
Given an integer n, generate the nth sequence.

Note: The sequence of integers will be represented as a string.

*/

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <stack>
#include <fstream>
#include <sstream>
#include "print.h"
using namespace std;


void testForStack()
{
	stack<int> mystack;
	mystack.push(10);
	mystack.push(20);
	mystack.top() -= 5;
	cout << "mystack.top() is now " << mystack.top() << endl;
}

void testForIntToString()
{
	int a = 10;
	stringstream ss;
	ss << a;
	string str = ss.str();
	cout <<str << endl;

	string str1 = to_string(a);
	
}


class Solution {
public:
	string countAndSay(int n) {
	
		string str1("1");
		if (n == 1)
		{
			return str1;
		}

		string preStr = str1;
		string result;

		for (int i = 1; i < n; i++)
		{
			int preCount = 0;
			string temp;
			string nextStr;
			temp = preStr[0];
			for (int j = 0; j < preStr.size(); j++)
			{
				if (temp == preStr.substr(j,1))
				{
					preCount++;
				}
				else
				{
					nextStr = nextStr + to_string(preCount) + temp;
					preCount = 1;
					temp = preStr.substr(j, 1);

				}
			}
			nextStr = nextStr + to_string(preCount) + temp;
			preStr = nextStr;
			result = nextStr;
		}
		return result;
	}
};
int main(int argc, char* argv[])
{
	
	testForIntToString();
	Solution s;
	int n;
	for (int i = 1; i < argc; i++){

		cout << argv[i] << endl;
		//arrInt[i - 1] = atoi(argv[i]);
		//str = argv[i];
		//charPtr = argv[i];
		//fileName = argv[i];
		n = atoi(argv[i]);

	}

	
	cout << "Count and Say:"<<s.countAndSay(n)<< endl;

	

	system("pause");
	return 0;
}